package com.team.abs.controller;

import java.sql.Timestamp;
import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.abs.entity.black_man;
import com.team.abs.entity.item_status;

import net.sf.json.JSONObject;
import com.team.abs.service.*;
import org.springframework.stereotype.Controller;
import com.team.abs.entity.white_kind;

@Controller
public class manager_controller {
	@GetMapping("/manager")
	public String item_status(){
		return "manager";
	}
	@GetMapping("/black_man")
	public String black_man(){
		return "black_man";
	}
	@GetMapping("/white_kind")
	public String white_kind(){
		return "white_kind";
	}
	@Autowired
	private black_white_service black_white_service;
	//将物品种类权限从白名单删除
	@ResponseBody
	@RequestMapping(value="/limit_item_api",method=RequestMethod.POST)
	public String limit_item(
			@RequestParam(value="limit_item_kind",required=true)String limit_item_kind
			){
		JSONObject result=new JSONObject();
		
		if(black_white_service.delete_white_kind(limit_item_kind)>0)
		result.put("status", "success");
		return result.toString();
	} 
	//将物品种类写入白名单
	@ResponseBody
	@RequestMapping(value="/unlimit_item_api",method=RequestMethod.POST)
	public String unlimit_item(
			@RequestParam(value="unlimit_item_kind",required=true)String unlimit_item_kind
			){
		JSONObject result=new JSONObject();
		
		if(black_white_service.insert_white_kind(unlimit_item_kind)>0)
		result.put("status", "success");
		return result.toString();
	} 
	//限制用户权限
	@ResponseBody
	@RequestMapping(value="/limit_man_api",method=RequestMethod.POST)
	public String limit_man(
			@RequestParam(value="limit_man",required=true)String limit_man,
			@RequestParam(value="limit_power",required=true)int limit_power

			){
		JSONObject result=new JSONObject();
		
		if(black_white_service.insert_black_man(new black_man( limit_power,limit_man))>0)
		result.put("status", "success");
		return result.toString();
	} 
	//解禁用户权限
	@ResponseBody
	@RequestMapping(value="/unlimit_man_api",method=RequestMethod.POST)
	public String limit_man(
			@RequestParam(value="unlimit_man",required=true)String unlimit_man
			){
		JSONObject result=new JSONObject();
		
		if(black_white_service.delete_balck_man(unlimit_man)>0)
		result.put("status", "success");
		return result.toString();
	} 
	//用户黑名单
	//设置value值确保没有和entity层重名
		@RequestMapping(value="/black")	
		public String search_black_man(Model model,@RequestParam(defaultValue = "1") Integer pageNum,
				@RequestParam(defaultValue = "8")Integer pageSize
				) {
			PageHelper.startPage(pageNum, pageSize);
				PageInfo<black_man> pageInfo =new PageInfo<>(black_white_service.search_all_black_man());
				//System.out.println(pageInfo.toString());
				model.addAttribute("pageNum", pageInfo.getPageNum());
				model.addAttribute("pageSize", pageInfo.getPageSize());
				model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
				model.addAttribute("isLastPage",pageInfo.isIsLastPage());
				model.addAttribute("totalPages",pageInfo.getPages());
				model.addAttribute("pageInfo",pageInfo.getList());
				model.addAttribute("total",pageInfo.getTotal());
				model.addAttribute("jumpUrl","/black?pageNum=");
			return "black_man";
		}
		//物品种类白名单
		@RequestMapping(value="/white")	
		public String search_white_kind(Model model,@RequestParam(defaultValue = "1") Integer pageNum,
				@RequestParam(defaultValue = "8")Integer pageSize
				) {
			PageHelper.startPage(pageNum, pageSize);
				PageInfo<white_kind> pageInfo =new PageInfo<>(black_white_service.search_all_white_kind());
				System.out.println(pageInfo.toString());
				model.addAttribute("pageNum", pageInfo.getPageNum());
				model.addAttribute("pageSize", pageInfo.getPageSize());
				model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
				model.addAttribute("isLastPage",pageInfo.isIsLastPage());
				model.addAttribute("totalPages",pageInfo.getPages());
				model.addAttribute("pageInfo",pageInfo.getList());
				model.addAttribute("total",pageInfo.getTotal());
				model.addAttribute("jumpUrl","/white?pageNum=");
			return "white_kind";
		}
}
